{% load static %}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title></title>
    <link rel="stylesheet" href="{% static 'js/lib/layui/css/layui.css' %}">
    <link rel="stylesheet" href="{% static 'css/common.css' %}">
</head>
<body>

<div class="layui-container" style="margin-top: 30px">
    <div class="layui-row">
        <div style="margin: 10px 0 20px;">
            <div style="margin-top: 10px;margin-bottom: 10px;">
                <label style="font-weight: bold;color: #333333">基础模型名称</label>
            </div>
            <input type="text" name="model_name" id="model_name" class="layui-input my-input">
        </div>
        <div style="margin: 10px 0 20px;">
            <div style="margin-top: 10px;margin-bottom: 10px;">
                <label style="font-weight: bold;color: #333333">请选择模型的类型</label>
            </div>
            <select name="model_type" id="model_type"
                    class="my-select" style="width: 100%">

                <option value="" style="color: gray;display: none;">请选择基础模型类型</option>
                <option value="数据挖掘模型">数据挖掘模型</option>
                <option value="知识推理模型">知识推理模型</option>
                <option value='关联分析模型'>关联分析模型</option>
            </select>
        </div>
        <div style="margin: 10px 0 20px;">
            <div style="margin-top: 10px;margin-bottom: 10px;">
                <label style="font-weight: bold;color: #333333">选择基础模型文件</label> <span style="color: #666666">&nbsp;&nbsp;(支持zip格式模型文件)</span>
            </div>
            <!-- 校验文件上传标志位 -->
            <input type="text" name="path" id="path" readonly hidden="hidden">
            <!-- 封装文件上传按钮 -->
            <input id="choose_file" type="file" multiple="multiple"
                   style="height: 0;width: 0;z-index: -1;" name="choosed_file"
                   onChange="return process_file(this)">
            <input id="hidden_btn" type="button" onclick="getElementById('choose_file').click()"
                   class="my-btn" style="position: relative;right: 3px;" value="选择模型文件">
            <!-- 文件名称显示标签 -->
            <span class="my-input-p" style="top:0px" id="file_name"></span>
        </div>
    </div>

    <div>
        <button type="submit" class="my-btn" style="float: right;" id="sub"> 确认</button>
    </div>
</div>

<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>
<script src="{% static 'js/lib/layui/layui.js' %}"></script>
<script>
    function process_file(obj) {
        $('#file_name').html($('#choose_file').val().split('\\').slice(-1))
    }

    $('#sub').click(function () {
        var model_name = $('#model_name').val();
        var model_type = $('#model_type').val();
        var file_name = $('#file_name').text();

        console.log(model_name, model_type, file_name);

        if (model_name === undefined) {
            alert("模型名称不能为空！！！");
        } else if (model_type === undefined) {
            alert("模型类型不能为空！！！");
        } else if(file_name === ""){
            alert("模型文件不能为空！！！");
        } else {
            $.ajax({
                url: '{% url 'add_base_model' %}',
                type: 'post',
                data: {'model_name': model_name, 'model_type': model_type},
                success: function (res) {
                    if (res["code"] == "1") {
                        window.parent.layer.closeAll();
                        window.parent.location.reload();
                    } else {
                        alert(res["msg"]);
                    }
                }
            })
        }
    });
</script>

</body>
</html>